package org.jeecg.modules.api.wlhy.customer;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.customer.entity.HyCustomerAddressSend;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerAddressSendService;
import org.jeecg.modules.wlhy.customer.service.IHyCustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
* @Description: hy_customer_address_send
* @Author: jeecg-boot
* @Date:   2020-12-02
* @Version: V1.0
*/
@Api(tags="货主发货地址")
@RestController
@RequestMapping("/api/wlhy/customer/sendaddress")
@Slf4j
public class ApiCustomerAddressSendController extends JeecgController<HyCustomerAddressSend, IHyCustomerAddressSendService> {
   @Autowired
   private IHyCustomerAddressSendService hyCustomerAddressSendService;

    @Autowired
    private IHyCustomerService hyCustomerService;
   /**
    * 分页列表查询
    *
    * @param hyCustomerAddressSend
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @ApiOperation(value="hy_customer_address_send-分页列表查询", notes="hy_customer_address_send-分页列表查询")
   @GetMapping(value = "/list")
   public Result<?> queryPageList(HyCustomerAddressSend hyCustomerAddressSend,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
       HyCustomer hyCustomer  = hyCustomerService.getCustomer(req);
       QueryWrapper<HyCustomerAddressSend> queryWrapper = QueryGenerator.initQueryWrapper(hyCustomerAddressSend, req.getParameterMap());
       queryWrapper.eq("customer_id",hyCustomer.getId());
       Page<HyCustomerAddressSend> page = new Page<HyCustomerAddressSend>(pageNo, pageSize);
       IPage<HyCustomerAddressSend> pageList = hyCustomerAddressSendService.page(page, queryWrapper);
       return Result.OK(pageList);
   }

//    @AutoLog(value = "hy_customer_address_send-分页列表查询")
    @ApiOperation(value="hy_customer_address_send-分页列表查询", notes="hy_customer_address_send-分页列表查询")
    @GetMapping(value = "/all")
    public Result<?> allList(HyCustomerAddressSend hyCustomerAddressSend,HttpServletRequest req) {
        QueryWrapper<HyCustomerAddressSend> queryWrapper = QueryGenerator.initQueryWrapper(hyCustomerAddressSend, req.getParameterMap());
        if (StrUtil.isNotEmpty(hyCustomerAddressSend.getCustomerId())) {
            queryWrapper.eq("customer_id", hyCustomerAddressSend.getCustomerId());
        } else {
            HyCustomer hyCustomer  = hyCustomerService.getCustomer(req);
            queryWrapper.eq("customer_id", hyCustomer.getId());
        }

        List<HyCustomerAddressSend> list = hyCustomerAddressSendService.list(queryWrapper);
        return Result.OK(list);
    }
   /**
    *   添加
    *
    * @param hyCustomerAddressSend
    * @return
    */
   @AutoLog(value = "hy_customer_address_send-添加")
   @ApiOperation(value="hy_customer_address_send-添加", notes="hy_customer_address_send-添加")
   @PostMapping(value = "/add")
   public Result<?> add(@RequestBody HyCustomerAddressSend hyCustomerAddressSend,HttpServletRequest req) {
       HyCustomer hyCustomer  = hyCustomerService.getCustomer(req);
       hyCustomerAddressSend.setCustomerId(hyCustomer.getId());
       hyCustomerAddressSendService.save(hyCustomerAddressSend);
       return Result.OK(hyCustomerAddressSend);
   }

   /**
    *  编辑
    *
    * @param hyCustomerAddressSend
    * @return
    */
   @AutoLog(value = "hy_customer_address_send-编辑")
   @ApiOperation(value="hy_customer_address_send-编辑", notes="hy_customer_address_send-编辑")
   @PutMapping(value = "/edit")
   public Result<?> edit(@RequestBody HyCustomerAddressSend hyCustomerAddressSend) {
       hyCustomerAddressSendService.updateById(hyCustomerAddressSend);
       return Result.OK("编辑成功!");
   }

   /**
    *   通过id删除
    *
    * @param id
    * @return
    */
   @AutoLog(value = "hy_customer_address_send-通过id删除")
   @ApiOperation(value="hy_customer_address_send-通过id删除", notes="hy_customer_address_send-通过id删除")
   @DeleteMapping(value = "/delete")
   public Result<?> delete(@RequestParam(name="id",required=true) String id) {
       hyCustomerAddressSendService.removeById(id);
       return Result.OK("删除成功!");
   }

   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   @ApiOperation(value="hy_customer_address_send-通过id查询", notes="hy_customer_address_send-通过id查询")
   @GetMapping(value = "/queryById")
   public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
       HyCustomerAddressSend hyCustomerAddressSend = hyCustomerAddressSendService.getById(id);
       if(hyCustomerAddressSend==null) {
           return Result.error("未找到对应数据");
       }
       return Result.OK(hyCustomerAddressSend);
   }

}
